<?php
switch ($_SERVER["REQUEST_METHOD"]){
    case "GET":
        get();
        break;
    case "POST":
        post();
        break;
}
function get(){
    echo <<<EOF
<!DOCTYPE html>
<html>
    <head>
        <title>藏头诗录入页面</title>    
        <style>
            i.hide{
                color:#00000000;
            }        
        </style>
    </head>
    <body>
        <h1>录入藏头诗</h1>
        <form action="#" method="post">
            <label for="keyword">关键词：</label>
            <input type="text" name="keyword" id="keyword" required><br>
            <label for="content">诗<i class="hide">口</i>句：</label>
            <textarea name="content" id="content" cols="30" rows="10"></textarea><br>
            <input type="submit">
        </form>   
        <p>
            API查询参数：<br>
            <code>keyword</code>：藏头诗关键词<br>
            <code>content</code>：藏头诗内容
        </p>  
    </body>
</html>
EOF;
}
function post(){
    $fn = "../ciku.json";
    touch($fn);
    $a = file_get_contents($fn);
    $a = empty($a)?[]:json_decode($a,true);
    $keyword=$_POST["keyword"];
    $content=$_POST["content"];
    $a[]=[$keyword,$content];
    $b=file_put_contents($fn,json_encode($a));
    if($b){
        echo "录入成功";
    }else{
        echo "录入失败";
    }
    echo '<script>setTimeout(()=>{window.open("/write","_self")},500);</script>';
}